﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using ASP;
using ASP.Models;

namespace ASP.Controllers
{
    /*
    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Extensions;
    using ASP;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<ProductDB>("ProductDBs");
    builder.EntitySet<Bacchu>("Bacchus"); 
    builder.EntitySet<Condition>("Conditions"); 
    builder.EntitySet<Hot6>("Hot6"); 
    builder.EntitySet<Material>("Materials"); 
    builder.EntitySet<Product_Mid>("Product_Mid"); 
    builder.EntitySet<ProductOut>("ProductOuts"); 
    builder.EntitySet<Vita500>("Vita500"); 
    config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    */
    public class ProductDBsController : ODataController
    {
        private ProductDBContext db = new ProductDBContext();

        // GET: odata/ProductDBs
        [EnableQuery]
        public IQueryable<ProductDB> GetProductDBs()
        {
            return db.ProductDBs;
        }

        // GET: odata/ProductDBs(5)
        [EnableQuery]
        public SingleResult<ProductDB> GetProductDB([FromODataUri] int key)
        {
            return SingleResult.Create(db.ProductDBs.Where(productDB => productDB.Pro_Id == key));
        }

        // PUT: odata/ProductDBs(5)
        public async Task<IHttpActionResult> Put([FromODataUri] int key, Delta<ProductDB> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ProductDB productDB = await db.ProductDBs.FindAsync(key);
            if (productDB == null)
            {
                return NotFound();
            }

            patch.Put(productDB);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductDBExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(productDB);
        }

        // POST: odata/ProductDBs
        public async Task<IHttpActionResult> Post(ProductDB productDB)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.ProductDBs.Add(productDB);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ProductDBExists(productDB.Pro_Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return Created(productDB);
        }

        // PATCH: odata/ProductDBs(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<ProductDB> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ProductDB productDB = await db.ProductDBs.FindAsync(key);
            if (productDB == null)
            {
                return NotFound();
            }

            patch.Patch(productDB);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductDBExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(productDB);
        }

        // DELETE: odata/ProductDBs(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] int key)
        {
            ProductDB productDB = await db.ProductDBs.FindAsync(key);
            if (productDB == null)
            {
                return NotFound();
            }

            db.ProductDBs.Remove(productDB);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        // GET: odata/ProductDBs(5)/Bacchus
        [EnableQuery]
        public IQueryable<Bacchu> GetBacchus([FromODataUri] int key)
        {
            return db.ProductDBs.Where(m => m.Pro_Id == key).SelectMany(m => m.Bacchus);
        }

        // GET: odata/ProductDBs(5)/Conditions
        [EnableQuery]
        public IQueryable<Condition> GetConditions([FromODataUri] int key)
        {
            return db.ProductDBs.Where(m => m.Pro_Id == key).SelectMany(m => m.Conditions);
        }

        // GET: odata/ProductDBs(5)/Hot6
        [EnableQuery]
        public IQueryable<Hot6> GetHot6([FromODataUri] int key)
        {
            return db.ProductDBs.Where(m => m.Pro_Id == key).SelectMany(m => m.Hot6);
        }

        // GET: odata/ProductDBs(5)/Materials
        [EnableQuery]
        public IQueryable<Material> GetMaterials([FromODataUri] int key)
        {
            return db.ProductDBs.Where(m => m.Pro_Id == key).SelectMany(m => m.Materials);
        }

        // GET: odata/ProductDBs(5)/Product_Mid
        [EnableQuery]
        public IQueryable<Product_Mid> GetProduct_Mid([FromODataUri] int key)
        {
            return db.ProductDBs.Where(m => m.Pro_Id == key).SelectMany(m => m.Product_Mid);
        }

        // GET: odata/ProductDBs(5)/ProductOuts
        [EnableQuery]
        public IQueryable<ProductOut> GetProductOuts([FromODataUri] int key)
        {
            return db.ProductDBs.Where(m => m.Pro_Id == key).SelectMany(m => m.ProductOuts);
        }

        // GET: odata/ProductDBs(5)/Vita500
        [EnableQuery]
        public IQueryable<Vita500> GetVita500([FromODataUri] int key)
        {
            return db.ProductDBs.Where(m => m.Pro_Id == key).SelectMany(m => m.Vita500);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ProductDBExists(int key)
        {
            return db.ProductDBs.Count(e => e.Pro_Id == key) > 0;
        }
    }
}
